<?php
	require_once( $C->INCPATH.'helpers/func_api.php' );
	require_once( $C->INCPATH.'classes/class_twitterdata.php' );
	
	$uri = $this->param('more');
	$format = $this->param('format');
	$rate_status = false;
	$oauth_status = false;
	$bauth_status = false;
	
	if( $auth = check_if_basic_auth() ) 
	{
		$user->logout();
		$res = $user->login( $auth[0], md5($auth[1]) );
		if( ! $res ) $oauth_error = 'Invalid Authorization header.';
		if($user->is_logged) $bauth_status = true;
	}
	elseif( ($auth = prepare_request()) || ($auth = prepare_header()) )
	{
		if(isset($auth['oauth_version']) && $auth['oauth_version'] != '1.0') $oauth_error = 'Not supported OAuth version';
		elseif(isset($auth['oauth_consumer_key'], $auth['oauth_nonce'], $auth['oauth_token'],$auth['oauth_signature_method'], $auth['oauth_signature'], $auth['oauth_timestamp']))
		{
			$ares = (isset($uri[1]))? ('/'.$uri[1]):'';
			$resource = (isset($uri[0]))? ('favorites/'.$uri[0].$ares):'favorites';

			$oauth_client = new OAuth($auth['oauth_consumer_key'], $auth['oauth_nonce'], $auth['oauth_token'], $auth['oauth_timestamp'], $auth['oauth_signature']);
				
			$oauth_client->set_variable('stage_url', $C->SITE_URL.'1/'.$resource.'.'.$format);
			if(isset($auth['oauth_version'])) $oauth_client->set_variable('version', '1.0');
			
			if($oauth_client->is_valid_get_resource_request())
			{
				if($auth['oauth_signature_method'] != 'HMAC-SHA1'){ $oauth_error = 'Unsupported signature method'; }
				elseif(!$oauth_client->decrypt_hmac_sha1()){ $oauth_error = 'Invalid signature'; }	
				//success
				else $oauth_status = true;
				//success
			}$oauth_error =  $oauth_client->get_variable('error_msg');		
		}else $oauth_error = 'Missing OAuth parameters';
	}
	
	$features = array('ids');
	if(isset($_GET['callback']) && valid_fn($_GET['callback'])) $callback = $_GET['callback'];
		else $callback = FALSE;

	if($_SERVER['REQUEST_METHOD'] != 'GET'){
		if(!isset($_REQUEST['suppress_response_codes'])) header('HTTP/1.1 400 Bad Request');
			else echo generate_error($format, 'Invalid request method.', $_SERVER['REQUEST_URI'], $callback);
		exit;
	}elseif(!is_valid_data_format($format, TRUE)){		
		if(!isset($_REQUEST['suppress_response_codes'])) header('HTTP/1.1 400 Bad Request');
			else echo generate_error('xml', 'Invalid data format requested.', $_SERVER['REQUEST_URI'], $callback);

		exit;
	}elseif(!isset($uri[0]) || !in_array($uri[0], $features)){
		if(!isset($_REQUEST['suppress_response_codes'])) header('HTTP/1.1 400 Bad Request');
			else echo generate_error($format, 'Invalid feature requested.', $_SERVER['REQUEST_URI'], $callback);
		exit;
	}elseif($uri[0] == 'ids')
	{
		if(isset($_GET['user_id']) && is_numeric($_GET['user_id'])) $id = intval($_GET['user_id']);
		elseif(isset($_GET['screen_name'])  || (isset($uri[1]) && !is_numeric($uri[1]))){
			if(isset($_GET['screen_name'])) $u = $this->network->get_user_by_username(urldecode($_GET['screen_name']));
				else $u = $this->network->get_user_by_username(urldecode($uri[1]));
			if(!$u){
				if(!isset($_GET['suppress_response_codes'])) header('HTTP/1.1 400 Bad Request');
					else echo generate_error($format, 'Invalid username.', $_SERVER['REQUEST_URI'], $callback);
				exit;
			}
			$id = $u->id;
		}elseif(isset($uri[1]) && is_numeric($uri[1])) $id = intval($uri[1]);
		else{
			if(!isset($_GET['suppress_response_codes'])) header('HTTP/1.1 400 Bad Request');
				else echo generate_error($format, 'Invalid username/id.', $_SERVER['REQUEST_URI'], $callback);
			exit;
		}
		
		if(!isset($id) && $oauth_status){
			$id = intval($oauth_client->get_field_in_table('oauth_access_token', 'user_id', 'access_token', urldecode($auth['oauth_token'])));
			if(!$id){ 
				if(!isset($_GET['suppress_response_codes'])) header('HTTP/1.1 400 Bad Request');
					else echo generate_error($format, 'Bad data provided.', $_SERVER['REQUEST_URI'], $callback);
				exit;
			}
		}
		if($oauth_status && $oauth_client->check_rate_limits($id)) $rate_status = true;
		elseif(check_rate_limits($_SERVER['REMOTE_ADDR'])) $rate_status = true;
		
		if(!$rate_status){
			if(!isset($_GET['suppress_response_codes'])) header('HTTP/1.1 400 Bad Request');
				else echo generate_error($format, 'You have no available rate limits, try again later.', $_SERVER['REQUEST_URI'], $callback);
			exit;
		}
		
		$info	= $this->network->get_user_follows($id);
		if(!$info) {
			if(!isset($_GET['suppress_response_codes'])) header('HTTP/1.1 500 Internal Server Error');
				else echo generate_error($format, 'Server error (Stage 1).', $_SERVER['REQUEST_URI'], $callback);
			exit;
		}
		$followers	= array_keys($info->followers);
		if(!count($followers)){
			if(!isset($_GET['suppress_response_codes'])) header('HTTP/1.1 304 Not Modified');
				else echo generate_error($format, 'No followers found.', $_SERVER['REQUEST_URI'], $callback);
			exit;
		}
		$num_rows = count($followers);
		
		$twitter_data = new TwitterData($format, $callback, $id, TRUE);
		$answer = $twitter_data->data_header();

		$answer .= $twitter_data->data_section('id_list', FALSE, FALSE, TRUE, ' type="array"');
			$answer .= $twitter_data->data_section('ids');			
			foreach($followers as $user_id)
			{ 
				$check = ($num_rows-1>0)? true:false;
				$answer .= $twitter_data->data_field('id', $user_id, $check);
				$num_rows--;	
			}		
			$answer .= $twitter_data->data_section('ids', FALSE, TRUE);
		$answer .= $twitter_data->data_section('id_list', FALSE,  TRUE, TRUE);
		$answer .= $twitter_data->data_bottom();
		
		echo $answer;
		exit;
	}
	
	if(!isset($_REQUEST['suppress_response_codes'])) header('HTTP/1.1 404 Not Found');
		else echo generate_error($format, 'Invalid resource request', $_SERVER['REQUEST_URI'], $callback);
	exit;	
?>